package com.tuanzi.loan.business.service.collaborate.impl;

import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelRatio;
import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelUser;
import com.tuanzi.loan.business.repository.collaborate.CollaborateChannelRatioRepository;
import com.tuanzi.loan.business.repository.collaborate.CollaborateChannelUserRepository;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateConfigListResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 14:57 2018/9/18
 * Description:
 */

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class CollaborateChannelUserServiceImpl extends CommonServiceImpl<NewCollaborateChannelUser, Integer>
        implements CollaborateChannelUserService {


    @Autowired
    private CollaborateChannelUserRepository collaborateChannelUserRepository;
    @Autowired
    private CollaborateChannelRatioRepository collaborateChannelRatioRepository;

    @Override
    protected JpaRepository<NewCollaborateChannelUser, Integer> getRepository() {
        return collaborateChannelUserRepository;
    }

    @Override
    public Page<CollaborateConfigListResponse> list(PageSearcher<CollaborateConfigListRequest> pageSearcher) {
        return collaborateChannelUserRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public CollaborateConfigListResponse findCollaborateConfigInfo(Integer channelUserId) {
        return collaborateChannelUserRepository.findCollaborateConfigInfo(channelUserId);
    }

    @Override
    public void saveUserChannelAndRatio(CollaborateConfigListResponse collaborateConfigListResponse) {
        String account = collaborateConfigListResponse.getAccount();
        Integer channelId = collaborateConfigListResponse.getChannelId();
        Integer tomorrowRatio = collaborateConfigListResponse.getTomorrowRatio();
        Integer showType = collaborateConfigListResponse.getShowType();

        NewCollaborateChannelUser channelUser = collaborateChannelUserRepository.findByChannelIdAndUserName(channelId, account);
        if(Objects.nonNull(channelUser)){
            channelUser.setChannelId(channelId);
            channelUser.setProductId(collaborateConfigListResponse.getProductId());
        }else {
            channelUser = new NewCollaborateChannelUser();
            channelUser.setUserName(account);
            channelUser.setChannelId(channelId);
            channelUser.setProductId(collaborateConfigListResponse.getProductId());
        }

        NewCollaborateChannelRatio ratioByChannel = collaborateChannelRatioRepository.findCollaborateChannelRatioByChannelId(channelId);
        if(Objects.nonNull(ratioByChannel)){
            ratioByChannel.setTomorrowRatio(tomorrowRatio);
            ratioByChannel.setShowType(showType);
            ratioByChannel.setMarketShow(collaborateConfigListResponse.getMarketShow());
            ratioByChannel.setCheckShow(collaborateConfigListResponse.getCheckShow());
        }else {
            ratioByChannel = new NewCollaborateChannelRatio();
            ratioByChannel.setRatio(tomorrowRatio);
            ratioByChannel.setTomorrowRatio(tomorrowRatio);
            ratioByChannel.setShowType(showType);
            ratioByChannel.setChannelId(channelId);
            ratioByChannel.setMarketShow(collaborateConfigListResponse.getMarketShow());
            ratioByChannel.setCheckShow(collaborateConfigListResponse.getCheckShow());
        }

        collaborateChannelUserRepository.save(channelUser);
        collaborateChannelRatioRepository.save(ratioByChannel);
    }
}
